//
//  CJBaseTextView.m
//  DatangLife
//
//  Created by 陈建 on 2018/6/27.
//  Copyright © 2018年 DatangLife. All rights reserved.
//

#import "CJBaseTextView.h"

@interface CJBaseTextView ()<UITextViewDelegate>

@end

@implementation CJBaseTextView

- (instancetype)init{
    self = [super init];
    if (self) {
        self.delegate = self;
        self.font = [UIFont systemFontOfSize:14.f];
        [self creatPlaceHolderLable];
    }
    return self;
    
}


- (void)setPlaceholder:(NSString *)placeholder{
    if (placeholder) {
        self.placeHolderLable.text = placeholder;
    }
}

- (void)creatPlaceHolderLable{
    UILabel *placeHolderLable = [[UILabel alloc] init];
    [self addSubview:placeHolderLable];
    self.placeHolderLable = placeHolderLable;
    placeHolderLable.textColor = [UIColor colorWithHexString:@"#C5C5CB"];
    placeHolderLable.text = @"请输入...";
    placeHolderLable.font = [UIFont systemFontOfSize:14.f];
    [placeHolderLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@5);
        make.top.equalTo(@8);
    }];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSLog(@"textLength == %ld",textView.text.length);
    if ([text isEqualToString:@"\n"]){
        //判断输入的字是否是回车，即按下return
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
    
}


- (void)textViewDidChange:(UITextView *)textView{
    
    
    if (textView.text.length == 0) {
        self.placeHolderLable.hidden = NO;
    }else{
        self.placeHolderLable.hidden = YES;
    }
    
    if(![textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length) {
        NSLog(@"输入为空");
        textView.text =[textView.text stringByReplacingOccurrencesOfString:@" " withString:@""];
        self.placeHolderLable.hidden = NO;
    }
    
    if (self.maxCount != 0) {
        NSInteger kMaxLength = self.maxCount;
        
        NSString *toBeString = textView.text;
        
        NSString *lang = [[UIApplication sharedApplication]textInputMode].primaryLanguage; //ios7之前使用[UITextInputMode currentInputMode].primaryLanguage
        
        if ([lang isEqualToString:@"zh-Hans"]) { //中文输入
            
            UITextRange *selectedRange = [textView markedTextRange];
            
            //获取高亮部分
            
            UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
            
            if (!position) {// 没有高亮选择的字，则对已输入的文字进行字数统计和限制
                
                if (toBeString.length > kMaxLength) {
                    
                    textView.text = [toBeString substringToIndex:kMaxLength];
                    
                }
                
            }
            
            else{//有高亮选择的字符串，则暂不对文字进行统计和限制
                
            }
            
        }else{//中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
            
            if (toBeString.length > kMaxLength) {
                
                textView.text = [toBeString substringToIndex:kMaxLength];
                
            }
        }
    }
    
    if ([self.delegateTextView respondsToSelector:@selector(CJ_textViewDidChange:)]) {
        [self.delegateTextView CJ_textViewDidChange:textView];
    }
    
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
